﻿package frEngine.core
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import baseEngine.utils.*;
    import flash.display.*;
    import flash.geom.*;

    public class FrLight3D extends Pivot3D
    {
        public var ambientColor:Vector.<Number>;
        public var type:int;
        public var color:Vector.<Number>;
        public var radius:Number = 0;
        public var attenuation:Number = 100;
        public var infinite:Boolean = true;
        public var multipler:Number = 1;
        public var sample:BitmapData;
        private static const _position:Vector3D = new Vector3D();
        public static const DIRECTIONAL:int = 0;
        public static const POINT:int = 1;

        public function FrLight3D(param1:String = "", param2:int = 1)
        {
            this.sample = new BitmapData(100, 100, false, 0);
            this.ambientColor = this.Vector.<Number>([0.6, 0.6, 0.6, 0]);
            this.color = this.Vector.<Number>([1, 1, 1, 0]);
            this.type = param2;
            this.setParams(16777215, 100, 1, 1, true);
            super(param1);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            this.sample.dispose();
            this.sample = null;
            return;
        }// end function

        override public function clone() : Pivot3D
        {
            var _loc_1:Pivot3D = null;
            var _loc_2:* = new FrLight3D(name, this.type);
            _loc_2.setParams(this.color[0] * 255 << 16 ^ this.color[1] * 255 << 8 ^ this.color[2] * 255, this.radius, this.attenuation, this.multipler, this.infinite);
            transform.copyToMatrix3D(_loc_2.transform);
            _loc_2.visible = visible;
            _loc_2.layer = layer;
            for each (_loc_1 in children)
            {
                
                _loc_2.addChild(_loc_1.clone());
            }
            return _loc_2;
        }// end function

        override public function get inView() : Boolean
        {
            if (!visible)
            {
                return false;
            }
            if (this.infinite)
            {
                return true;
            }
            world.copyColumnTo(3, _position);
            Matrix3DUtils.transformVector(Device3D.view, _position, _position);
            priority = this.infinite || this.type == DIRECTIONAL ? (-10000000) : (_position.z / Global3D.camera.far * 100000);
            var _loc_1:* = 1 / Global3D.camera.zoom / _position.z;
            var _loc_2:* = Global3D.scene.viewPort.width / Global3D.scene.viewPort.height;
            if (_position.length > this.radius)
            {
                if ((_position.x + this.radius) * _loc_1 < -0.5)
                {
                    return false;
                }
                if ((_position.x - this.radius) * _loc_1 > 0.5)
                {
                    return false;
                }
                if ((_position.y + this.radius) * _loc_1 * _loc_2 < -0.5)
                {
                    return false;
                }
                if ((_position.y - this.radius) * _loc_1 * _loc_2 > 0.5)
                {
                    return false;
                }
                if (_position.z - this.radius > Global3D.camera.far)
                {
                    return false;
                }
                if (_position.z + this.radius < Global3D.camera.near)
                {
                    return false;
                }
            }
            return true;
        }// end function

        public function setParams(param1:int = 16777215, param2:Number = 0, param3:Number = 1, param4:Number = 1, param5:Boolean = false) : void
        {
            if (this.type == DIRECTIONAL)
            {
                param5 = true;
            }
            this.radius = param2;
            this.attenuation = param3;
            var _loc_6:* = 1 / 255;
            this.color[0] = ((param1 & 16711680) >> 16) * _loc_6;
            this.color[1] = ((param1 & 65280) >> 8) * _loc_6;
            this.color[2] = (param1 & 255) * _loc_6;
            this.infinite = param5;
            this.multipler = param4;
            var _loc_7:Array = [param1, param1, param1, 0];
            var _loc_8:Array = [];
            var _loc_9:* = param2 * param2;
            _loc_8.push(0, 0);
            if (param5)
            {
                _loc_8.push(255, 255);
            }
            else
            {
                _loc_8.push((1 - param3) * (1 - param3) * param2 * param2 / _loc_9 * 253);
                _loc_8.push(param2 * param2 / _loc_9 * 253);
                if (_loc_8[2] >= _loc_8[3])
                {
                    _loc_8[2] = _loc_8[3] - 1;
                }
            }
            this.setColors(this.sample, _loc_7, null, _loc_8);
            var _loc_10:* = param4 / 5;
            this.sample.colorTransform(this.sample.rect, new ColorTransform(_loc_10, _loc_10, _loc_10));
            return;
        }// end function

        private function setColors(param1:BitmapData, param2:Array, param3:Array = null, param4:Array = null) : void
        {
            var _loc_5:* = new Shape();
            var _loc_6:* = new Matrix();
            _loc_6.createGradientBox(param1.width, param1.height);
            _loc_5.graphics.beginGradientFill(GradientType.LINEAR, param2, param3, param4, _loc_6, SpreadMethod.PAD, InterpolationMethod.RGB);
            _loc_5.graphics.drawRect(0, 0, param1.width, param1.height);
            param1.draw(_loc_5);
            return;
        }// end function

    }
}
